package com.yjc.drygoods.order.web;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yjc.common.utils.R;
import com.yjc.drygoods.order.entity.OrderEntity;
import com.yjc.drygoods.order.enume.OrderStatusEnum;
import com.yjc.drygoods.order.feign.WareFeignService;
import com.yjc.drygoods.order.vo.OrderSubmitVo;
import com.yjc.drygoods.order.service.OrderService;
import com.yjc.drygoods.order.vo.OrderConfirmVo;
import com.yjc.drygoods.order.vo.OrderSubmitRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.concurrent.ExecutionException;

/**
 * @author yjc
 * @create 2022-09-23 19:50
 */
@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @Autowired
    WareFeignService wareFeignService;

    /**
     * 完成订单
     *
     * @param orderSn
     * @return
     */
    @GetMapping("/finish/order/{orderSn}")
    public String finishOrder(@PathVariable("orderSn") String orderSn) {
        //1.扣除真实库存
        R r = wareFeignService.finishRealStock(orderSn);
        if (r.getCode() == 0) {
            //2.更改订单状态
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setOrderSn(orderSn);
            orderEntity.setConfirmStatus(1);
            orderEntity.setReceiveTime(new Date());
            orderEntity.setStatus(OrderStatusEnum.RECEIVED.getCode());
            orderService.update(orderEntity, new UpdateWrapper<OrderEntity>().eq("order_sn", orderSn));
        }
        return "redirect:http://member.drygoods.top/orderList.html";
    }

    /**
     * 取消订单
     *
     * @param orderSn
     * @return
     */
    @GetMapping("/close/order/{orderSn}")
    public String closeOrder(@PathVariable("orderSn") String orderSn) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(orderSn);
        orderService.closeOrder(orderEntity);
        return "redirect:http://member.drygoods.top/orderList.html";
    }

    @GetMapping("/{page}.html")
    public String listPage(@PathVariable("page") String page) {
        return page;
    }

    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirms = orderService.confirmOrder();

        model.addAttribute("orderConfirm", orderConfirms);
        return "confirm";
    }

    @PostMapping("/submitOrderInfo")
    public String submitOrderInfo(OrderSubmitVo submitVo, Model model, RedirectAttributes re) {
        //下单：创建订单，验令牌，验价格，锁库存
        //下单成功跳转到支付界面
        //下单失败则跳转回结算页重新确认订单信息
//        System.out.println(submitVo);
        OrderSubmitRespVo orderSubmitResp = null;
        try {
            orderSubmitResp = orderService.submitOrder(submitVo);
            if (orderSubmitResp.getCode() == 0) {
                model.addAttribute("submitOrderResp", orderSubmitResp.getOrder());
                return "pay";
            } else {
                switch (orderSubmitResp.getCode()) {
                    //令牌不通过
                    case 1:
                        re.addFlashAttribute("msg", "订单通行码过时，请重新刷新界面");
                        break;
                    //订单应付金额与结算页应付金额不同
                    case 2:
                        re.addFlashAttribute("msg", "订单应付金额与结算页应付金额不同");
                        break;
                }
                return "redirect:http://order.drygoods.top/toTrade";
            }
        } catch (Exception e) {
            re.addFlashAttribute("msg", e.getMessage());
            return "redirect:http://order.drygoods.top/toTrade";
        }

    }


}
